const { pinyin}=require('../../lib/Convert_Pinyin.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:'',
    fl:null,
    fls:null,
    popShow:false, //右上角点击添加的弹框
    popShow2:false, //删除按钮弹框
    flag:false,
    showSearch:false,
    curIndex:0,
    fuserInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getFriends();
  },

  /**
   * 获取好友列表
   */
  getFriends(){
    wx.cloud.callFunction({
      name:'friend',
      data:{
        act:'get'
      }
    }).then(res=>{
      //console.log(res.result);
      let fl=res.result.fl;
      let openid=res.result.openid;
      this.setData({
        fl,
        openid
      })
    })
  },


  toPhb(){
    wx.switchTab({
      url: '../phb/phb'
    })
  },

  /**
   * 右上角的添加弹窗控制
   */
  popChange(){
    this.setData({
      flag:true,
      popShow:!this.data.popShow,
    })
  },

  /**
   * 从现有好友筛选
   */
  searchLocal(e){
    let val=e.detail.value;
    this.setData({
      showSearch: !(val=='')
    });
    val = val.replace(/\\/g, '\\\\').replace(/\./g, '\\.').replace(/\*/g, '\\*');
    let reg = new RegExp(val, "i"); //创建正则对象用于匹配，不区分大小写
    let fl=this.data.fl;
    let fls=[];

    //循环好友列表找出符合输入的好友
    for(let i=fl.length-1;i>=0;i--){
      //根据输入的字直接匹配
      let name=fl[i].userInfo.nickName;
      if(reg.test(name)){
        fls.unshift(fl[i]);
        continue;
      }
      //如果直接匹配没匹配则匹配拼音
      let namePyf = pinyin.getFullChars(name);
      if(reg.test(namePyf)){
        fls.unshift(fl[i]);
        continue;
      }
      //最后匹配拼音首字母缩写
      let namePys = pinyin.getCamelChars(name);
      if (reg.test(namePys)) {
        fls.unshift(fl[i]);
        continue;
      }
    }
    this.setData({
      fls
    });
  },

  toSearchFriend(e){
    wx.navigateTo({
      url: '../searchFriend/searchFriend',
    })
  },

  /**
   * 显示删除弹窗
   */
  showPopdel:function(e){
    //console.log(e);
    let i=e.currentTarget.dataset.index;
    const {x,y}=e.detail;
    let item=this.data.fl[i];
    wx.getSystemInfo({
      success:(res)=> {
        //console.log(res);
        let w=res.windowWidth;
        let h=res.windowHeight;
        let ds1 = (x > w / 2) ? 'right:' + (w - x) + 'px;' : 'left:' + x + 'px;';
        let ds2 = (y > h / 2) ? 'bottom:' + (h - y) + 'px;' : 'top:' + y + 'px;';
        let ds=ds2+ds1;
        item.ds=ds;
        this.setData({
          ['fl[' + i + ']']: item,
          popShow2:true,
          curIndex:i
        });
        //console.log(this.data.fl[i]);
      },
    })
  },

  /**
   * 隐藏删除弹窗
   */
  hidePopdel(){
    let fl=this.data.fl;
    let i=this.data.curIndex;

    this.setData({
      ['fl[' + i + '].ds']: null,
      popShow2:false
    })
    //console.log(this.data.fl);
  },

  delFriend:function(e){
    //console.log(e.currentTarget.dataset);
    wx.showModal({
      title: '提示',
      content: '确认删除该好友',
      success:res=>{
        if(res.confirm){
          wx.showLoading({
            title: '正在删除',
          });
          const { index, openid } = e.currentTarget.dataset;
          
          //云函数执行删除操作
          wx.cloud.callFunction({
            name:'friend',
            data:{
              fopenid:openid,
              act:'del'
            }
          }).then(res1=>{
            if(res1.result.code==0){
              this.getFriends();
            }
            wx.hideLoading();
            wx.showToast({
              title: res1.result.mes,
            });
          })
        }
        if(res.cancel){
          this.hidePopdel();
        }
      }
    })
  },

  sendFriInfo(e) {
    let i = e.currentTarget.dataset.index;
    let fuserInfo = this.data.fl[i].userInfo;
    //console.log(fuserInfo);
    this.setData({
      fuserInfo
    })
    this.selectComponent('#popgift2').showModel();

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (){
    return {
      title:'来和我一起运动吧',
      path:'pages/index/index?openid='+this.data.openid
    }
  }
})